function TreeNode(val) {
    this.val = val;
    this.left = this.right = null;
}

// 递归的解法
var mirrorTree = function(root) {
    // 首先判断边界条件
    if (root == null) return null;
    change(root);
    return root;

    // 递归函数进行判断
    function change(node) {
        if (node != null) {
            let temp = node.left;
            node.left = node.right;
            node.right = temp;
        }else {
            return
        }
        change(node.left)
        change(node.right)
    }
}
// 栈的实现
var mirrorTree = function(root) {
    /*
    从根节点开始入栈 然后取出栈顶的元素 并将她们的左右子树都依次压入栈 同时交换左右子树 直到栈空为止
    * */
    // 首先是边界条件的判断
    if (root == null) return null
    // 创建栈
    let stack = []
    stack.push(root)
    // return stack
    while (stack.length != 0) {
        // 栈顶的元素
        let node = stack.pop()
        if (node.left != null) stack.push(node.left)
        if (node.right != null) stack.push(node.right)
        // 交换
        let  temp = node.left
        node.left = node.right
        node.right = temp
    }
    return root
}
let root = {
    val : 4,
    left : {
        val:2,
        left:1,
        right: 3,
    },
    right: {
        val: 7,
        left: 6,
        right: 9,
    },
}
// console.log(root)
console.log(mirrorTree(root))